package com.yangjie.common.dao.criteria;

import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;

import com.yangjie.common.StringUtils;

public class UnaryExpression extends AbstractCriteriaExpression {
	public UnaryExpression(String field, String op) {
		this.field = field;
		this.op = op;
	}

	public UnaryExpression(String field, String op, String value) {
		this.field = field;
		this.op = op;
		this.value = value;
	}

	private Object value = null;

	public Object getValue() {
		return value;
	}

	public void setValue(Object value) {
		this.value = value;
	}

	public Criterion toCriterion() {
		if(value instanceof String) {
			if(StringUtils.isEmptyString((String)value)) {
				return null;
			}
		}
		if (CriteriaExpressionIF.OP_EQUAL.equalsIgnoreCase(this.getOp())) {
			return Restrictions.eq(this.getField(), value);
		} else if (CriteriaExpressionIF.OP_GT.equalsIgnoreCase(this.getOp())) {
			return Restrictions.gt(this.getField(), value);
		} else if (CriteriaExpressionIF.OP_GTANDEQ.equalsIgnoreCase(this
				.getOp())) {
			return Restrictions.ge(this.getField(), value);
		} else if (CriteriaExpressionIF.OP_LIKE.equalsIgnoreCase(this.getOp())) {
			return Restrictions.like(this.getField(), value);
		} else if (CriteriaExpressionIF.OP_LT.equalsIgnoreCase(this.getOp())) {
			return Restrictions.lt(this.getField(), value);
		} else if (CriteriaExpressionIF.OP_LTANDEQ.equalsIgnoreCase(this
				.getOp())) {
			return Restrictions.le(this.getField(), value);
		} else if (CriteriaExpressionIF.OP_NOTEQ.equalsIgnoreCase(this.getOp())) {
			return Restrictions.ne(this.getField(), value);
		} else {
			return null;
		}
	}

}
